﻿// ———————————————————————–
// <copyright file="Definition.cs" company="The MITRE Corporation">
//    Copyright (c) 2014 The MITRE Corporation. All rights reserved.
//    Licensed under the Apache License, Version 2.0 (the "License");
//    you may not use this file except in compliance with the License.
//    You may obtain a copy of the License at
//    http://www.apache.org/licenses/LICENSE-2.0
//    Unless required by applicable law or agreed to in writing, software
//    distributed under the License is distributed on an "AS IS" BASIS,
//    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
//    See the License for the specific language governing permissions and
//    limitations under the License.
// </copyright>
// ———————————————————————–
/////////////////////////////////////////////////////////////////////////////////////////////////
// Definition.cs:
//
// 
//
// Project: IC.NET - CAD Library
// Author:  Jim Cronin - MITRE
/////////////////////////////////////////////////////////////////////////////////////////////////

// Copyright (c) 2014 The MITRE Corporation. All rights reserved.
//
// NOTICE
// This software was produced for the U. S. Government
// under Contract No. FA8721-09-C-0001, and is
// subject to the Rights in Noncommercial Computer Software
// and Noncommercial Computer Software Documentation Clause
// (DFARS) 252.227-7014 (JUN 1995)

using System;
using System.Xml;

namespace EDXLSharp.FEMARTLT
{
  public abstract class Definition : ComplexTypeBase
  {
    public string Id;
    public int? Tier;
    public string Name;
    public string Status;
    public string StatusDescription;
    public DateTime? Updated;
    public DateTime? Released;
    public string Discipline;
    public int? DisciplineNumber;
    public string PrimaryCoreCapability;
    public string SecondaryCoreCapability;

    /// <summary>
    /// Writes this Object to an inline XML Document
    /// </summary>
    /// <param name="xwriter">Valid XMLWriter</param>
    /// <exception cref="System.ArgumentNullException">Thrown if xwriter is null</exception>
    /// <exception cref="EDXLSharp.ValidationException">Thrown if the object fails to validate</exception>
    public override void WriteXML(XmlWriter xwriter)
    {
      if (xwriter == null)
      {
        throw new ArgumentNullException("xwriter");
      }

      this.Validate();

      xwriter.WriteStartElement(ElementPrefix, ElementName, ElementNamespace);
      WriteInnerXML(xwriter);
      xwriter.WriteEndElement();
    }

    /// <summary>
    /// Writes the inner content of the XML element representing this object
    /// </summary>
    /// <param name="xwriter">Valid XMLWriter</param>
    /// <exception cref="System.ArgumentNullException">Thrown if xwriter is null</exception>
    protected virtual void WriteInnerXML(XmlWriter xwriter)
    {
      if (xwriter == null)
      {
        throw new ArgumentNullException("xwriter");
      }

      if (!string.IsNullOrEmpty(Id))
      {
        xwriter.WriteElementString("Id", EDXLConstants.FEMARTLTNamespace, Id);
      }
      if (Tier.HasValue)
      {
        xwriter.WriteElementString("Tier", EDXLConstants.FEMARTLTNamespace, Tier.Value.ToString());
      }
      if (!string.IsNullOrEmpty(Name))
      {
        xwriter.WriteElementString("Name", EDXLConstants.FEMARTLTNamespace, Name);
      }
      if (!string.IsNullOrEmpty(Status))
      {
        xwriter.WriteElementString("Status", EDXLConstants.FEMARTLTNamespace, Status);
      }
      if (!string.IsNullOrEmpty(StatusDescription))
      {
        xwriter.WriteElementString("StatusDescription", EDXLConstants.FEMARTLTNamespace, StatusDescription);
      }
      if (Updated.HasValue)
      {
        xwriter.WriteElementString("Updated", EDXLConstants.FEMARTLTNamespace, Updated.Value.ToUniversalTime().ToString("yyyy-MM-ddTHH:mm:ss.ffZ"));
      }
      if (Released.HasValue)
      {
        xwriter.WriteElementString("Released", EDXLConstants.FEMARTLTNamespace, Released.Value.ToUniversalTime().ToString("yyyy-MM-ddTHH:mm:ss.ffZ"));
      }
      if (!string.IsNullOrEmpty(Discipline))
      {
        xwriter.WriteElementString("Discipline", EDXLConstants.FEMARTLTNamespace, Discipline);
      }
      if (DisciplineNumber.HasValue)
      {
        xwriter.WriteElementString("DisciplineNumber", EDXLConstants.FEMARTLTNamespace, DisciplineNumber.Value.ToString());
      }
      if (!string.IsNullOrEmpty(PrimaryCoreCapability))
      {
        xwriter.WriteElementString("PrimaryCoreCapability", EDXLConstants.FEMARTLTNamespace, PrimaryCoreCapability);
      }
      if (!string.IsNullOrEmpty(SecondaryCoreCapability))
      {
        xwriter.WriteElementString("SecondaryCoreCapability", EDXLConstants.FEMARTLTNamespace, SecondaryCoreCapability);
      }
    }

    /// <summary>
    /// Reads this Object's values from an XML nodelist
    /// </summary>
    /// <param name="rootnode">root XML Node of the Object element</param>
    /// <exception cref="System.ArgumentNullException">Thrown if rootnode is null</exception>
    /// <exception cref="System.ArgumentException">Thrown if the rootnode is not the correct type</exception>
    /// <exception cref="EDXLSharp.ValidationException">Thrown if the object fails to validate</exception>
    public override void ReadXML(XmlNode rootnode)
    {
      if (rootnode == null)
      {
        throw new ArgumentNullException("rootnode");
      }

      if (rootnode.LocalName != ElementName)
      {
        throw new ArgumentException("Unexpected element " + rootnode.LocalName + " instead of " + ElementName);
      }

      foreach (XmlNode node in rootnode.ChildNodes)
      {
        ReadNode(node);
      }

      this.Validate();
    }

    protected virtual void ReadNode(XmlNode node)
    {
      switch (node.LocalName)
      {
        case "Id":
          Id = node.InnerText;
          break;
        case "Tier":
          Tier = int.Parse(node.InnerText);
          break;
        case "Name":
          Name = node.InnerText;
          break;
        case "Status":
          Status = node.InnerText;
          break;
        case "StatusDescription":
          StatusDescription = node.InnerText;
          break;
        case "Updated":
          Updated = DateTime.Parse(node.InnerText);
          break;
        case "Released":
          Released = DateTime.Parse(node.InnerText);
          break;
        case "Discipline":
          Discipline = node.InnerText;
          break;
        case "DisciplineNumber":
          DisciplineNumber = int.Parse(node.InnerText);
          break;
        case "PrimaryCoreCapability":
          PrimaryCoreCapability = node.InnerText;
          break;
        case "SecondaryCoreCapability":
          SecondaryCoreCapability = node.InnerText;
          break;
        default:
          throw new ArgumentException("Unexcpted element " + node.LocalName + " in " + ElementName);
      }
    }

    /// <summary>
    /// Validates This Message element For Required Values and Conformance
    /// </summary>
    /// <exception cref="EDXLSharp.ValidationException">Thrown if the object fails to validate</exception>
    public override void Validate()
    {
    }
  }
}